{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import geoviews as gv\n",
    "import geoviews.feature as gf\n",
    "import cartopy.crs as ccrs\n",
    "\n",
    "gv.extension('bokeh')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Declare data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def sample_data(shape=(20, 30)):\n",
    "    \"\"\"\n",
    "    Returns ``(x, y, u, v, crs)`` of some vector data\n",
    "    computed mathematically. The returned crs will be a rotated\n",
    "    pole CRS, meaning that the vectors will be unevenly spaced in\n",
    "    regular PlateCarree space.\n",
    "\n",
    "    \"\"\"\n",
    "    crs = ccrs.RotatedPole(pole_longitude=177.5, pole_latitude=37.5)\n",
    "\n",
    "    x = np.linspace(311.9, 391.1, shape[1])\n",
    "    y = np.linspace(-23.6, 24.8, shape[0])\n",
    "\n",
    "    x2d, y2d = np.meshgrid(x, y)\n",
    "    u = 10 * (2 * np.cos(2 * np.deg2rad(x2d) + 3 * np.deg2rad(y2d + 30)) ** 2)\n",
    "    v = 20 * np.cos(6 * np.deg2rad(x2d))\n",
    "\n",
    "    return x, y, u, v, crs\n",
    "\n",
    "xs, ys, U, V, crs = sample_data()\n",
    "mag = np.sqrt(U**2 + V**2)\n",
    "angle = (np.pi/2.) - np.arctan2(U/mag, V/mag)\n",
    "vectorfield = gv.VectorField((xs, ys, angle, mag), crs=crs)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Plot"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "gf.ocean * gf.coastline * gf.land * vectorfield.opts(\n",
    "    magnitude='Magnitude', width=500, height=500,\n",
    "    projection=ccrs.Orthographic(-10, 45), global_extent=True)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
